
#include "IAlignment.h"

using namespace BWE;

IAlignment::IAlignment()
{
	ia_form = Form_None;
	ia_align = Align_None;
	ia_spacing = 1;
}
IAlignment::~IAlignment()
{

}

BRect IAlignment::adjust(const BRect& area)
{
	ia_hintSize.reset();
	ia_textRect.reset();
	ia_imageRect.reset();
	ia_textArea.reset();

	Form realForm = ia_form;
	if (ia_form == Form_None)
	{
		if (ia_imageSize.valid() && ia_textSize.valid())
			realForm = Form_IconText;
		else
		{
			if (ia_imageSize.valid())
				realForm = Form_IconOnly;
			if (ia_textSize.valid())
				realForm = Form_TextOnly;
		}
	}
	if (realForm == Form_IconAlone)
	{
		if (ia_imageSize.empty())
			realForm = Form_TextOnly;
		else
			realForm = Form_IconOnly;
	}
	if (realForm == Form_TextAlone)
	{
		if (ia_textSize.empty())
			realForm = Form_IconOnly;
		else
			realForm = Form_TextOnly;
	}
	switch (realForm)
	{
	case Form_IconOnly:
	{
		ia_imageArea = area;
		ia_hintSize = ia_imageSize;
		BRect rect = ia_imageArea.align(ia_align, ia_imageSize);
		ia_imageRect = rect;
		return rect;
	}
	case Form_TextOnly:
	{
		ia_textArea = area;
		ia_hintSize = ia_textSize;
		BRect rect = ia_textArea.align(ia_align, ia_textSize);
		ia_textRect = rect;
		return rect;
	}
	case Form_IconText:
	{
		ia_hintSize = BSize(ia_imageSize.width() + ia_spacing + ia_textSize.width(), bMax(ia_imageSize.height(), ia_textSize.height()));
		BRect rect = area.align(ia_align, ia_hintSize);

		ia_imageArea = rect;
		ia_imageArea.setWidth(ia_imageSize.width());
		ia_imageRect = ia_imageArea.align(ia_align, ia_imageSize);

		ia_textArea = rect;
		ia_textArea.left() = rect.right() - ia_textSize.width();
		ia_textRect = ia_textArea.align(ia_align, ia_textSize);

		return rect;
	}
	case Form_TextIcon:
	{
		ia_hintSize = BSize(ia_imageSize.width() + ia_spacing + ia_textSize.width(), bMax(ia_imageSize.height(), ia_textSize.height()));
		BRect rect = area.align(ia_align, ia_hintSize);

		ia_textArea = rect;
		ia_textArea.setWidth(ia_textSize.width());
		ia_textRect = ia_textArea.align(ia_align, ia_textSize);

		ia_imageArea = rect;
		ia_imageArea.left() = rect.right() - ia_imageSize.width();
		ia_imageRect = ia_imageArea.align(ia_align, ia_imageSize);

		return rect;
	}
	case Form_TextUnderIcon:
	{
		ia_hintSize = BSize(bMax(ia_imageSize.width(), ia_textSize.width()), ia_imageSize.height() + ia_spacing + ia_textSize.height());
		BRect rect = area.align(ia_align, ia_hintSize);

		ia_imageArea = rect;
		ia_imageArea.setHeight(ia_imageSize.height());
		ia_imageRect = ia_imageArea.align(ia_align, ia_imageSize);

		ia_textArea = rect;
		ia_textArea.top() = rect.top() + ia_imageSize.height() + ia_spacing;
		ia_textRect = ia_textArea.align(ia_align, ia_textSize);

		return rect;
	}
	case Form_IconUnderText:
	{
		ia_hintSize = BSize(bMax(ia_imageSize.width(), ia_textSize.width()), ia_imageSize.height() + ia_spacing + ia_textSize.height());
		BRect rect = area.align(ia_align, ia_hintSize);

		ia_textArea = rect;
		ia_textArea.setHeight(ia_textSize.height());
		ia_textRect = ia_textArea.align(ia_align, ia_textSize);

		ia_imageArea = rect;
		ia_imageArea.top() = rect.top() + ia_textSize.height() + ia_spacing;
		ia_imageRect = ia_imageArea.align(ia_align, ia_imageSize);
	}
	}
	return area;
}
